<?php
/**
*+-----------IMBA后台栏目管理控制器----------------+
*+-------------------------------------------------+
*+-----------主要是视频栏目专题的控制管理----------+
*@author susy.liang <liangling@ilongyuan.com.cn>
*+-------------------------------------------------+
*/
namespace Admin\Controller;

class TopicController extends BaseManageController {
	/**
	*控制操作的模型表
	*/
	protected $modelName = 'Topic';	

	/**
	*删除类型的设置 true|直接删除， false|设置数据状态
	*/	
	protected $is_del	= false;

	/**
	*跳转的Url
	*/
	protected $jumpAct	= 'Topic/index';

	/**
	*操作index的前置操作
	*---设置数据筛选条件---
	*/
	public function _before_index()
	{
		$map = array();//筛选条件
        $map['status'] = array('egt',0);

		$search = trim(I('get.name', '', 'strip_tags'));
        if($search){
            $map['title']    =   array('like', (string)$search);
        }

        $this->where = $map;
	}

	/**
	*栏目视频推荐
	*@param int $topicId 栏目标识
	*/
	public function recommend($id){
		$this->id = $id = intval($id);
		if(IS_POST){
			$data = I('');
			if(empty($data))
			{
				// $this->error('请选择视频项');
			}

			$res = $this->model->updateVideo($id, $data);
			if($res === true)
				$this->success('视频推荐成功', U('Topic/recommend?id='.$id));
			else
				$this->error('视频推荐失败', U('Topic/recommend?id='.$id));
		}else{
			$info = $this->model->find($id);
			if(empty($info))
				$this->error('请操作合法的栏目');

			$this->data = $info;

			$videos = D('Common/Topic', 'Service')->getVideos($id);
			if($videos){
				
				$temp = array();
				foreach ($videos as $key => $value) {
					$temp[$value['id']] = $value;
				}
				$videos = $temp;

				if($info['videos']){
					$selectedIds = array_map('intval', explode(',', $info['videos']) );
					$temp = array();
					foreach ($selectedIds as $key => $value) {
						if($videos[$value])
							$temp[] = $videos[$value];
					}
					// $where['id'] = array('in', $selectedIds);
					// $selectedV = D('Common/Video', 'Service')->where($where)->select();
					$selectedV = $temp;
				}
			}
			
			$this->videos = $videos;
			$this->selectedViedo = $selectedV;
			$this->display('video');
		}
	}

}